<?php

class CashbackAction extends CommonAction
{
	function _initialize(){
		parent::_initialize();
		$this->dao = D('cashback_count');// 编辑、删除操作需要
	}
	public function index(){
		$this->dao = D('cashback_count');
		$this->listmap .= " and site_id=" . $this->siteid;
        parent::_list("","",$field="id,site_id,member_id,total,rest");
        $this->display();
	}
	// 返现总额详情
	public function detail(){
		$member_id = $_GET['id'];

		$this->dao = D('sale');
		$this->listmap = null;
		$this->listmap['s.site_id'] = $this->siteid;
		$this->listmap['s.member_id'] = $member_id;
		$this->listmap['p.is_cashback'] = 1;

		parent::_list("s","__PRODUCT__ as p on p.id=s.product_id","s.id,s.member_id,p.title,s.num,s.total,s.order_id,s.product_id");
		$this->display();
	}
	public function config(){
		$this->dao = D('cashback_config');
		$siteid = $this->siteid;
		if(!empty($siteid)){
			$map['site_id'] = $siteid;
			
			$map['id'] = (int)$this -> _get('id');
			
			
            $info = $this->info = $this->dao->where($map)->find();
		}else{
            $this->error('参数错误');
		}
		if(IS_POST){
			if($this->dao->create()){
				if(empty($info)){
					$result = $this->dao->add();
				}else{
					$result = $this->dao->save();
				}
				if($result === false){
					$this->error("数据库错误");
				}else{
					$this->success("保存成功",U('index'));
				}
			}else{
				$this->error($this->dao->getError());
			}
			
		}else{
            $this->display();

		}
	}
	// 返现日志
	public function logList(){
		// $Log = D('Member_account_log');
		$this->dao = D('Member_account_log');

		// 根据用户ID搜索
		$map['member_id'] = $_GET['id'];
		// 没有记录商品的价格，代表这是分成记录或返现记录
		$map['goodsMoney'] = 0;
		$map['remark'] = array('like',"%返现%");
		// $map['p.is_cashback'] = 1;

		$sort="id desc";

		// $Log->_list($view,$map,$sort);
		$this->listmap = $map;
		$this->_list("","","id,site_id,member_id,changenum,dateline");

        $this->display();
	}

	// public function test1(){
	// 	echo "测试Cashback 2.添加返现商品标记（完成）3.根据返现商品，增加返现余额(完成）";
	// 	$order_id = "94";
	// 	$saleDB = M('sale');
	// 	$sale = $saleDB->alias('s')->join("left join __PRODUCT__ as p on p.id=s.product_id")->where(array('s.order_id'=>$order_id))->select();
	// 	// dump($sale);
	// 	// 返现				  
	//     import("@.ORG.Cashback.Cashback");
	//     // 判断是否返现
	//     // foreach($sale as $s){
	//     $s = $sale[0];
	//     $s['is_cashback'] = 1;
	//     	$Cashback = new Cashback($s);
	//      	if($Cashback->canCashback()){
	//      		echo "可以返现";
	//      		// 增加返现总额、余额操作
	//     		$Cashback->addMoney();
	//      	}else{
	//      		echo "不可以返现";
	//      	}
	//      // }

	// }
	// public function test2(){
	// 	echo "测试Cashback 根据日返率和返现余额计算返现金，增加到余额中";
	// 	$id = "1";
	// 	$Count = D("CashbackCount");
	// 	$Log = D("Member_account_log");
	// 	if($change_money = $Count->change($id)){
	// 	// if(true){
	// 		// $Log->change(29.97,"31229","每日返现");
	// 		$Log->change($change_money,$Count->member_id,$Count->config['remark']);//注意，这里remark一定要出现返现二字，不然可能搜索不到日志结果
	// 	}
	// }
	// public function test3(){
	// 	echo "测试Cashback 根据返现时间间隔进行日返率调整";
	// 	$id = "1";
	// 	$Count = D("CashbackCount");
	// 	$Count->adjust($id);
	// }
	// public function cashbcakByDay(){
	// 	// echo "测试Cashback 批量返现金操作";
	// 	$Count = D("CashbackCount");
	// 	$Count->cashbackByDay();
	// }
	// public function adjustByDay(){
	// 	// echo "测试Cashback 批量日返率调整";
	// 	$Count = D("CashbackCount");
	// 	$Count->adjustByDay();
	// }
	// public function daily(){
	// 	$this->cashbackByDay();
	// 	$this->adjustByDay();
	// }
	
	public function _list(){
		//计算总页数
		$count=$this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->count();
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$p=new Page($count,$this->pagesize);
		$this->list = $this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name, c.name as cate_name')->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
'.$p->listRows)->select();
        $this->pages=$p->show();
	}
	
	function selectProduct(){
		$this->dao = D('Product');
		$this->listmap="1 and n.site_id=".$this->siteid;
		$this->listorder="sort,id";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords; 
		}
		$relateids = $this->dao -> where(array('id' => $this->_get('product_id'), 'site_id' => $this->siteid)) -> getField('relateids');
		$this->selectedid = explode(',', $relateids);
		//dump($selectedid);
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		$this->display();
	}
	
}